export default {
	data() {
		return {
			isRegister: false,
			backType: "relaunch",
			backPage: "pages/index/index",
			backPageQuery: "",
			isAgree: false,
		}
	},
	onLoad(options) {
		this.isRegister = options.isRegister ? JSON.parse(options.isRegister):false
		this.backType = options.backType || "relaunch"
		this.backPage = decodeURIComponent(options.backPage || "pages/index/index")
		this.backPageQuery = decodeURIComponent(options.backPageQuery || "")
	},
	methods: {
		navigateBack: function() {
			console.log("navigateBack", this.backPage)
			if(this.backType === 'back') {
				let pages = getCurrentPages();
				let delta = 1
				if(pages) {
					for(let i=0; i<pages.length; i++) {
						if(pages[i].route == this.backPage) {
							delta = pages.length - 1 - i
							break
						}
					}
				}
				
				uni.navigateBack({
					delta: delta
				})
			} else if(this.backType === 'redirect') {
				uni.redirectTo({
					url: this.backPageQuery ? ("/" + this.backPage + "?" + this.backPageQuery):("/" + this.backPage)
				})
			} else {
				uni.reLaunch({
					url: this.backPageQuery ? ("/" + this.backPage + "?" + this.backPageQuery):("/" + this.backPage)
				})
			}
		},
		toServiceAgreement: function(event) {
			uni.navigateTo({
				url: "/pages/service-agreement/service-agreement"
			})
		},
		toPrivacyPolicies: function(event) {
			uni.navigateTo({
				url: "/pages/privacy-policies/privacy-policies"
			})
		},
		switchAgreementChecked: function(event) {
			this.isAgree = !this.isAgree
		},
		navigateTo({url = "", query = {}} = {}) {
			let pages = getCurrentPages()
			let currentPage = null
			if(pages && pages.length > 0) {
				currentPage = pages[pages.length - 1]
			}
			let pageQuery = ""
			if(currentPage) {
				let options = currentPage ? (currentPage.options || {}):{}
				let list = []
				
				for(let key in query) {
					if(Object.keys(options).indexOf(key) == -1) {
						list.push(key + "=" + (query[key] || ''))
					}
				}
				
				for(let key in options) {
					list.push(key + "=" + (options[key] || ''))
				}
				pageQuery = list.join("&")
			}
			uni.navigateTo({
				url: pageQuery ? (url + "?" + pageQuery) : url
			})
		}
	}
}